package client.gui;

/**
 * @author primianoc
 *
 */

import javax.bluetooth.BluetoothStateException;
import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.JButton;

import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JSlider;

import client.audio.PlayerManager;
import client.container.BtPortContainer;
import client.container.FileCompletedContainer;
import client.event.BtPortContainerListener;
import client.event.BtPortEvent;
import client.event.NewDataReceivedEvent;
import client.event.NewPositionEvent;
import client.event.NewSenderEvent;
import client.event.NewStreamEvent;
import client.event.PlayerManagerEvent;
import client.event.PlayerManagerListener;
import client.event.PortProviderListener;
import client.event.SessionManagerListener;
import client.event.StreamErrorEvent;
import client.net.ISessionManager;
import client.net.PortProvider;
import client.net.SessionManager;

import com.intel.bluetooth.BlueCoveImpl;
import commonUtility.JPanelImage;

import java.awt.Font;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import java.awt.SystemColor;
import javax.swing.border.TitledBorder;
import javax.swing.JProgressBar;
import javax.swing.ImageIcon;

public class JFrameClient extends JFrame implements BtPortContainerListener, 
SessionManagerListener, PortProviderListener, PlayerManagerListener
{
	private PortProvider _psmProvider = null;
	private Vector<PlayerManager> _playerManagerVector = new Vector<PlayerManager>();  //  @jve:decl-index=0:
	private boolean _updateJSlider = true;
	private Timer _timer = null;
	private Object _synchPlayerManager = new Object();  //  @jve:decl-index=0:
	
	private static final long serialVersionUID = 1L;
	private JPanel _jContentPane = null;
	private JPanelImage _jPanelBackground = null;
	private JPanel _jPanelPlayer = null;
	private JPanelImage _jPanelImageBig = null;
	private JTextArea _jTextGuide = null;
	private JButton _jButtonGuide2 = null;
	private JPanelImage _jPanelImage2 = null;
	private JPanelImage _jPanelImage4 = null;
	private JButton _jButtonGuide4 = null;
	private JButton _jButtonGuide1 = null;
	private JLabel _jLabelPorts = null;
	private JScrollPane _jScrollPanePorts = null;
	private JList _jListPorts = null;
	private JLabel _jLabel1BtId = null;
	private JComboBox _jComboBoxBTDevice = null;
	private JCheckBox _jCheckBoxEnable = null;
	private JLabel _jLabelGwSender = null;
	private JLabel _jLabelDataPort = null;
	private JPanel _jPanelAdmin = null;
	private JButton _jButtonGuide3 = null;
	private JPanelImage _jPanelImage3 = null;
	private JScrollPane _jScrollPaneTextGuide = null;
	private JPanelImage _jPanelImageUser0 = null;
	private JPanelImage _jPanelImageUser4 = null;
	private JPanelImage _jPanelImageUser2 = null;
	private JPanelImage _jPanelImageUser3 = null;
	private JPanelImage _jPanelImage1 = null;
	private JToggleButton _jToggleButtonAdmin = null;
	private JSlider _jSliderTime = null;
	private JSlider _jSliderGain = null;
	private JButton _jButtonPlay = null;
	private JButton _jButtonClose = null;
	private JLabel _jLabelStreamName = null;
	private JLabel _jLabelTime = null;
	private PlayerManager _playerManager = null;  //  @jve:decl-index=0:
	private JLabel _jLabelVolume = null;
	private JPanelImage _jPanelImage0 = null;
	private JPanelImage _jPanelImageUser1 = null;
	private JProgressBar _jProgressBarBuffer = null;
	private int _jProgressBarX;
	private int _jProgressBarWidth;
	private JScrollPane _jScrollPaneGwSender = null;
	private JTextArea _jTextAreaGwSender = null;
	private JProgressBar _jProgressBarBufferBack = null;
	private JPanelImage _jPanelImageError1 = null;
	private JPanelImage _jPanelImageError2 = null;
	private JPanelImage _jPanelImageError3 = null;
	private JPanelImage _jPanelImageError4 = null;
	private JPanelImage _jPanelImageWaiting1 = null;
	private JPanelImage _jPanelImageWaiting2 = null;
	private JPanelImage _jPanelImageWaiting3 = null;
	private JPanelImage _jPanelImageWaiting4 = null;
	
	
	/**
	 * This is the default constructor
	 */
	public JFrameClient()
	{
		super();
		initialize();

		/**
		 *  Aggiornamenti del container delle porte disponibili.
		 */
		BtPortContainer.getInstance().addBtPortEventListener(this);
		getJListPorts().setListData(BtPortContainer.getInstance().getAllBtPort());
		
		getJPanelPlayer().setVisible(false);
		_timer = new Timer();
		
		/**
		 * Posizione iniziale sulla mappa.
		 */
		setPosition("file0");
		

	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(1107, 567);
		this.setMinimumSize(new Dimension(1107, 567));
		this.setEnabled(true);
		this.setIconImage(Toolkit.getDefaultToolkit()
				.getImage("./media/logo.png"));
		
		/**
		 * Set Look & Feel
		 */
		try 
		{
			javax.swing.UIManager
			.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		}
		catch(Exception e) 
		{
			e.printStackTrace();
		}
		this.setContentPane(getJContentPane());
		this.setTitle("BRADiPO - Client");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	
	/**
	 * @return the _updateJSlider
	 */
	public boolean isUpdateJSlider()
	{
		return _updateJSlider;
	}

	/**
	 * @param updateJSlider the _updateJSlider to set
	 */
	public void setUpdateJSlider(boolean updateJSlider)
	{
		_updateJSlider = updateJSlider;
	}

	/**
	 * This method initializes _jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (_jContentPane == null)
		{
			_jContentPane = new JPanel();
			_jContentPane.setLayout(null);
			_jContentPane.add(getJPanelBackground(), null);
			_jContentPane.add(getJScrollPaneTextGuide(), null);
			_jContentPane.add(getJPanelAdmin(), null);
			_jContentPane.add(getJPanelImageBig(), null);
			_jContentPane.setComponentZOrder(getJPanelImageBig(), 0);
			_jContentPane.add(getJPanelPlayer(), null);
			_jContentPane.add(getJToggleButtonAdmin(), null);
		}
		return _jContentPane;
	}

	/**
	 * This method initializes _jPanelBackground	
	 * 	
	 * @return testgui.JPanelImage	
	 */
	private JPanelImage getJPanelBackground()
	{
		if (_jPanelBackground == null)
		{
			_jLabelDataPort = new JLabel();
			_jLabelDataPort.setText("Data Port");
			_jLabelDataPort.setFont(new Font("DejaVu Sans", Font.PLAIN, 10));
			_jLabelDataPort.setBounds(new Rectangle(230, 6, 53, 20));
			_jLabelGwSender = new JLabel();
			_jLabelGwSender.setText("GW Sender");
			_jLabelGwSender.setFont(new Font("DejaVu Sans", Font.PLAIN, 10));
			_jLabelGwSender.setBounds(new Rectangle(131, 6, 86, 20));
			_jLabel1BtId = new JLabel();
			_jLabel1BtId.setText("BT ID");
			_jLabel1BtId.setFont(new Font("DejaVu Sans", Font.PLAIN, 10));
			_jLabel1BtId.setBounds(new Rectangle(76, 5, 34, 18));
			_jLabelPorts = new JLabel();
			_jLabelPorts.setText("Ports");
			_jLabelPorts.setFont(new Font("DejaVu Sans", Font.PLAIN, 10));
			_jLabelPorts.setBounds(new Rectangle(8, 5, 39, 17));
			_jPanelBackground = new JPanelImage();
			_jPanelBackground.setAbsolutePathImage("./media/mappa.jpg");
			_jPanelBackground.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.yellow, 3), "", TitledBorder.LEFT, TitledBorder.BELOW_TOP, new Font("DejaVu Sans Condensed", Font.BOLD, 12), SystemColor.activeCaption));
			_jPanelBackground.setBounds(new Rectangle(260, 4, 836, 398));
			_jPanelBackground.setName("panelBackground");
			_jPanelBackground.add(getJPanelImage2(), null);
			_jPanelBackground.add(getJButtonGuide2(), null);
			_jPanelBackground.add(getJPanelImage4(), null);
			_jPanelBackground.add(getJButtonGuide4(), null);
			_jPanelBackground.add(getJButtonGuide1(), null);
			_jPanelBackground.add(getJButtonGuide3(), null);
			_jPanelBackground.add(getJPanelImage3(), null);
			_jPanelBackground.add(getJPanelImageUser0(), null);
			_jPanelBackground.add(getJPanelImageUser4(), null);
			_jPanelBackground.add(getJPanelImageUser2(), null);
			_jPanelBackground.add(getJPanelImageUser3(), null);
			_jPanelBackground.add(getJPanelImage1(), null);
			_jPanelBackground.add(getJPanelImage0(), null);
			_jPanelBackground.add(getJPanelImageUser1(), null);
			_jPanelBackground.add(getJPanelImageError1(), null);
			_jPanelBackground.add(getJPanelImage22(), null);
			_jPanelBackground.add(getJPanelImageError3(), null);
			_jPanelBackground.add(getJPanelImage42(), null);
			_jPanelBackground.add(getJPanelImageWaiting1(), null);
			_jPanelBackground.add(getJPanelImageWaiting2(), null);
			_jPanelBackground.add(getJPanelImageWaiting3(), null);
			_jPanelBackground.add(getJPanelImageWaiting4(), null);
		}
		return _jPanelBackground;
	}

	
	/**
	 * This method initializes _jSliderTime	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	public JSlider getJSliderTime()
	{
		if (_jSliderTime == null)
		{
			_jSliderTime = new JSlider();
			_jSliderTime.setPaintTicks(true);
			_jSliderTime.setToolTipText("Slider posizione");
			_jSliderTime.setMajorTickSpacing(60);
			_jSliderTime.setMinorTickSpacing(10);
			_jSliderTime.setFont(new Font("Dialog", Font.PLAIN, 8));
			_jSliderTime.setMaximum(300);
			_jSliderTime.setBounds(new Rectangle(6, 78, 237, 41));
			_jSliderTime.setValue(0);
			_jSliderTime.setPaintLabels(true);
			_jSliderTime.addMouseListener(new java.awt.event.MouseAdapter()
			{   
				public void mouseReleased(java.awt.event.MouseEvent e) 
				{    
					if(e.getComponent().isEnabled() == true && _playerManager != null)
					{
						int startTime = ((JSlider)e.getSource()).getValue();

						_playerManager.setStartTime(startTime);
						setUpdateJSlider(true);
					}
				}
				
				public void mousePressed(java.awt.event.MouseEvent e)
				{
					if(e.getComponent().isEnabled() == true && _playerManager != null)
					{
						setUpdateJSlider(false);
					}
				}
			});
		}
		return _jSliderTime;
	}

	/**
	 * This method initializes _jSliderGain	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	public JSlider getJSliderGain()
	{
		if (_jSliderGain == null)
		{
			_jSliderGain = new JSlider();
			_jSliderGain.setPaintTicks(true);
			_jSliderGain.setMaximum(200);
			_jSliderGain.setMajorTickSpacing(100);
			_jSliderGain.setMinorTickSpacing(10);
			_jSliderGain.setBounds(new Rectangle(3, 20, 51, 57));
			_jSliderGain.setPaintLabels(true);
			_jSliderGain.setFont(new Font("DejaVu Sans", Font.PLAIN, 8));
			_jSliderGain.setOrientation(JSlider.VERTICAL);
			_jSliderGain.addChangeListener(new javax.swing.event.ChangeListener()
			{
				public void stateChanged(javax.swing.event.ChangeEvent e)
				{
					if(_playerManager != null)
					{
						int gainValue = ((JSlider)e.getSource()).getValue();
						_playerManager.setGainLevel(gainValue);
					}
				}
			});
		}
		return _jSliderGain;
	}

	/**
	 * This method initializes _jButtonPlay	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonPlay()
	{
		if (_jButtonPlay == null)
		{
			_jButtonPlay = new JButton();
			_jButtonPlay.setText("Play");
			_jButtonPlay.setBounds(new Rectangle(81, 36, 78, 37));
			_jButtonPlay.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mouseClicked(java.awt.event.MouseEvent e)
				{
					if(e.getComponent().isEnabled() == true && _playerManager != null)
					{
						if(_playerManager.isPlayerPlaying() == false)
						{
							_playerManager.play();
							_jButtonPlay.setText("Pause");
						}
						else
						{
							_playerManager.pause();
							_jButtonPlay.setText("Play");
						}
					}
					
				}
			});
		}
		return _jButtonPlay;
	}

	/**
	 * This method initializes _jButtonClose	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonClose()
	{
		if (_jButtonClose == null)
		{
			_jButtonClose = new JButton();
			_jButtonClose.setBorderPainted(true);
			_jButtonClose.setHorizontalTextPosition(SwingConstants.CENTER);
			_jButtonClose.setHorizontalAlignment(SwingConstants.CENTER);
			_jButtonClose.setMargin(new Insets(0, 0, 0, 0));
			_jButtonClose.setBorder(null);
			_jButtonClose.setActionCommand(null);
			_jButtonClose.setToolTipText("Close");
			_jButtonClose.setLocation(new Point(207, 7));
			_jButtonClose.setSize(new Dimension(35, 35));
			_jButtonClose.setEnabled(true);
			_jButtonClose.setFont(new Font("DejaVu Sans", Font.PLAIN, 10));
			_jButtonClose.setText("X");
			_jButtonClose.setVisible(true);
			_jButtonClose.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mouseClicked(java.awt.event.MouseEvent e)
				{
					if(e.getComponent().isEnabled() == true)
					{
						closeJPanelPlayer(_playerManager.getStreamName());
					}
					
				}


			});
		}
		return _jButtonClose;
	}
	
	private void closeJPanelPlayer(String streamName)
	{

		PlayerManager playerManager = getPlayerManager(streamName);

		/**
		 * Chiudo il PlayerManger.
		 */
		if(playerManager != null)
		{
			
			/**
			 * Chiudo il SessionManager con lo stesso nome del Player rimuovendolo
			 * dal PsmProvider.
			 */
			if(_psmProvider != null)
			{
				_psmProvider.removeSessionManager(playerManager.getStreamName());
			}

			if(FileCompletedContainer.getInstance().
					isStreamCompleted(playerManager.getStreamName()) == false)
			{
				jButtonEnable(playerManager.getStreamName(), false);

				playerManager.close();

				synchronized(_synchPlayerManager)
				{
					_playerManagerVector.remove(playerManager);
				}

			}
			else
			{
				playerManager.stopPlayer();
			}
			
			getJPanelImage(playerManager.getStreamName() + "-waiting").setVisible(false);
		}
		
		if(_playerManager != null && _playerManager.equals(playerManager))
		{
			_playerManager = null;
			getJPanelPlayer().setVisible(false);

			resetJProgressBarBuffer();
		}

	}
	
	/**
	 * This method initializes _jPanelPlayer	
	 * 	
	 * @return testgui.JPanelPlayer	
	 */
	private JPanel getJPanelPlayer()
	{
		if (_jPanelPlayer == null)
		{
			_jPanelPlayer = new JPanel();
			_jLabelVolume = new JLabel();
			_jLabelVolume.setBounds(new Rectangle(7, 4, 34, 12));
			_jLabelVolume.setFont(new Font("DejaVu Sans", Font.PLAIN, 8));
			_jLabelVolume.setText("Volume");
			_jLabelTime = new JLabel();
			_jLabelTime.setText("00:00:00");
			_jLabelTime.setBounds(new Rectangle(183, 59, 58, 19));
			_jLabelStreamName = new JLabel();
			_jLabelStreamName.setToolTipText("Name");
			_jLabelStreamName.setBounds(new Rectangle(80, 7, 81, 19));
			_jLabelStreamName.setText("Name");
			_jPanelPlayer.setSize(248, 124);
			_jPanelPlayer.setLayout(null);

			_jPanelPlayer.setMinimumSize(new Dimension(228, 100));
			_jPanelPlayer.setMaximumSize(new Dimension(228, 100));
			_jPanelPlayer.add(getJSliderTime(), null);
			_jPanelPlayer.setComponentZOrder(getJSliderTime(), 0);
			_jPanelPlayer.add(getJSliderGain(), null);
			_jPanelPlayer.add(getJButtonPlay(), null);
			_jPanelPlayer.add(getJButtonClose(), null);
			_jPanelPlayer.add(_jLabelStreamName, null);
			_jPanelPlayer.add(_jLabelTime, null);
			_jPanelPlayer.add(_jLabelVolume, null);
			_jPanelPlayer.setEnabled(true);
			_jPanelPlayer.setBounds(new Rectangle(6, 410, 248, 124));
			_jPanelPlayer.setBorder(BorderFactory.createTitledBorder(BorderFactory.
					createLineBorder(Color.blue, 3), "", TitledBorder.DEFAULT_JUSTIFICATION, 
					TitledBorder.ABOVE_TOP, new Font("DejaVu Sans", Font.BOLD, 10), SystemColor.activeCaption));
			_jPanelPlayer.setVisible(true);
			_jPanelPlayer.add(getJProgressBarBuffer(), null);
			_jPanelPlayer.setComponentZOrder(getJProgressBarBuffer(), 1);
			_jPanelPlayer.add(getJProgressBarBufferBack(), null);
			_jPanelPlayer.setComponentZOrder(getJProgressBarBufferBack(), 2);
		}
		return _jPanelPlayer;
	}

	/**
	 * This method initializes _jPanelImageBig	
	 * 	
	 * @return testgui.JPanelImage	
	 */
	private JPanelImage getJPanelImageBig()
	{
		if (_jPanelImageBig == null)
		{
			_jPanelImageBig = new JPanelImage();
			_jPanelImageBig.setName("imgBig");
			_jPanelImageBig.setBorder(null);
			_jPanelImageBig.setBounds(new Rectangle(597, 212, 490, 323));
		}
		return _jPanelImageBig;
	}

	/**
	 * This method initializes _jTextGuide	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextGuide()
	{
		if (_jTextGuide == null)
		{
			_jTextGuide = new JTextArea();
			_jTextGuide.setLineWrap(true);
			_jTextGuide.setWrapStyleWord(true);
			_jTextGuide.setBorder(null);
			_jTextGuide.setEditable(false);
		}
		return _jTextGuide;
	}

	/**
	 * This method initializes _jButtonGuide2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonGuide2()
	{
		if (_jButtonGuide2 == null)
		{
			_jButtonGuide2 = new JButton();
			_jButtonGuide2.setText("Audio");
			_jButtonGuide2.setName("file2");
			_jButtonGuide2.setEnabled(false);
			_jButtonGuide2.setFont(new Font("DejaVu Sans", Font.PLAIN, 12));
			_jButtonGuide2.setAutoscrolls(false);
			_jButtonGuide2.setOpaque(true);
			_jButtonGuide2.setIcon(new ImageIcon("./media/audio.png"));
			_jButtonGuide2.setBounds(new Rectangle(208, 243, 94, 40));
			_jButtonGuide2.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mouseClicked(java.awt.event.MouseEvent e)
				{
					if(e.getComponent().isEnabled() == true)
					{
						mouseClickedButtonGuide(e.getComponent().getName());
					}
				}
			});
		}
		return _jButtonGuide2;
	}

	/**
	 * This method initializes _jPanelImage2	
	 * 	
	 * @return testgui.JPanelImage	
	 */
	private JPanelImage getJPanelImage2()
	{
		if (_jPanelImage2 == null)
		{
			_jPanelImage2 = new JPanelImage();
			_jPanelImage2.setBounds(new Rectangle(68, 200, 129, 87));
			_jPanelImage2.setName("file2-thumbnail");
			_jPanelImage2.setAbsolutePathImage("./media/file2.jpg");
		}
		return _jPanelImage2;
	}

	/**
	 * This method initializes _jPanelImage4	
	 * 	
	 * @return testgui.JPanelImage	
	 */
	private JPanelImage getJPanelImage4()
	{
		if (_jPanelImage4 == null)
		{
			_jPanelImage4 = new JPanelImage();
			_jPanelImage4.setBounds(new Rectangle(738, 25, 82, 117));
			_jPanelImage4.setName("file4-thumbnail");
			_jPanelImage4.setAbsolutePathImage("./media/file4.jpg");
		}
		return _jPanelImage4;
	}

	/**
	 * This method initializes _jButtonGuide3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonGuide4()
	{
		if (_jButtonGuide4 == null)
		{
			_jButtonGuide4 = new JButton();
			_jButtonGuide4.setBounds(new Rectangle(637, 111, 94, 40));
			_jButtonGuide4.setName("file4");
			_jButtonGuide4.setEnabled(false);
			_jButtonGuide4.setFont(new Font("DejaVu Sans", Font.PLAIN, 12));
			_jButtonGuide4.setIcon(new ImageIcon("./media/audio.png"));
			_jButtonGuide4.setText("Audio");
			_jButtonGuide4.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mouseClicked(java.awt.event.MouseEvent e)
				{
					if(e.getComponent().isEnabled() == true)
					{
						mouseClickedButtonGuide(e.getComponent().getName());
					}
				}
			});
		}
		return _jButtonGuide4;
	}

	/**
	 * This method initializes _jButtonGuide1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonGuide1()
	{
		if (_jButtonGuide1 == null)
		{
			_jButtonGuide1 = new JButton();
			_jButtonGuide1.setName("file1");
			_jButtonGuide1.setEnabled(false);
			_jButtonGuide1.setFont(new Font("DejaVu Sans", Font.PLAIN, 12));
			_jButtonGuide1.setIcon(new ImageIcon("./media/audio.png"));
			_jButtonGuide1.setIconTextGap(1);
			_jButtonGuide1.setBounds(new Rectangle(125, 127, 94, 40));
			_jButtonGuide1.setText("Audio");
			_jButtonGuide1.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mouseClicked(java.awt.event.MouseEvent e)
				{
					if(e.getComponent().isEnabled() == true)
					{
						mouseClickedButtonGuide(e.getComponent().getName());
					}
				}
			});
		}
		return _jButtonGuide1;
	}

	
	/**
	 * This method initializes _jScrollPanePorts	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPanePorts()
	{
		if (_jScrollPanePorts == null)
		{
			_jScrollPanePorts = new JScrollPane();
			_jScrollPanePorts.setBounds(new Rectangle(3, 22, 61, 98));
			_jScrollPanePorts.setViewportView(getJListPorts());
		}
		return _jScrollPanePorts;
	}

	/**
	 * This method initializes _jListPorts	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJListPorts()
	{
		if (_jListPorts == null)
		{
			_jListPorts = new JList();
			_jListPorts.setSize(new Dimension(50, 61));
			_jListPorts.setFont(new Font("DejaVu Sans", Font.PLAIN, 10));
		}
		return _jListPorts;
	}

	/**
	 * This method initializes _jComboBoxBTDevice	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxBTDevice()
	{
		if (_jComboBoxBTDevice == null)
		{
			String[] deviceID = configComboBoxDeviceID();
			
			if(deviceID == null)
			{
				deviceID = new String[] { "NO BT" };
			}
			
			ComboBoxModel jComboBoxChooseDeviceIDModel = 
				new DefaultComboBoxModel(deviceID);
			
			_jComboBoxBTDevice = new JComboBox();
			_jComboBoxBTDevice.setBounds(new Rectangle(73, 25, 41, 24));
			_jComboBoxBTDevice.setModel(jComboBoxChooseDeviceIDModel);
		}
		return _jComboBoxBTDevice;
	}

	/**
	 * This method initializes _jCheckBoxEnable	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxEnable()
	{
		if (_jCheckBoxEnable == null)
		{
			_jCheckBoxEnable = new JCheckBox();
			_jCheckBoxEnable.setHorizontalTextPosition(SwingConstants.CENTER);
			_jCheckBoxEnable.setPreferredSize(new Dimension(40, 40));
			_jCheckBoxEnable.setLocation(new Point(72, 64));
			_jCheckBoxEnable.setSize(new Dimension(44, 48));
			_jCheckBoxEnable.setText("Enable");
			_jCheckBoxEnable.setVerticalTextPosition(SwingConstants.TOP);
			_jCheckBoxEnable.setFont(new Font("DejaVu Sans", Font.PLAIN, 10));
			_jCheckBoxEnable.setHorizontalAlignment(SwingConstants.CENTER);
			_jCheckBoxEnable.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					if(e.getStateChange() == ItemEvent.SELECTED)
					{
						String deviceID = (String)getJComboBoxBTDevice().getSelectedItem();
						
						if(deviceID.equalsIgnoreCase("NO BT"))
						{
							return;
						}
						
						/**
						 *  Se il checkbox e' stato selezionato abilito il 
						 *  thread PortProvider.
						 */
						_psmProvider = new PortProvider(getSessionManagerListener(), deviceID);
						_psmProvider.setName("Thread PsmProvider");
						_psmProvider.addPortProviderListener(getPortProviderListener());
						_psmProvider.start();
						
					}
					else 
					{
						if(e.getStateChange() == ItemEvent.DESELECTED && _psmProvider != null)
						{
							_playerManager = null;
							
							synchronized(_synchPlayerManager)
							{
								for (PlayerManager playerManager: _playerManagerVector)
								{
									playerManager.close();
									
									// Chiudo anche il SessionManager che ha lo stesso
									// nome del playerManger
									_psmProvider.removeSessionManager(playerManager.getStreamName());
								}

								_playerManagerVector.clear();
							}

							
							_psmProvider.removePortProviderListener(getPortProviderListener());
							
							_psmProvider.setProviderActive(false);
							
							//Il thread potrebbe essere bloccato in attesa di una connessione
							//quindi richiamo il metodo interrupt per terminarlo
							_psmProvider.interrupt();
							
							try
							{
								_psmProvider.join();
							} 
							catch (InterruptedException e1)
							{
								e1.printStackTrace();
							}
							
							_psmProvider = null;
							
							/*
							 * Reset del pannello player e della posizione sulla mappa.
							 */
							resetPanelPlayer();
							jButtonEnable("ALLBUTTON", false);
							getJTextAreaGwSender().setText("");
							setPosition("file0");
							FileCompletedContainer.getInstance().reset();
							getJPanelPlayer().setVisible(false);
							resetJProgressBarBuffer();
						}
					}
				}
			});
		}
		return _jCheckBoxEnable;
	}

	/**
	 * This method initializes _jPanelAdmin	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelAdmin()
	{
		if (_jPanelAdmin == null)
		{
			_jPanelAdmin = new JPanel();
			_jPanelAdmin.setLayout(null);
			_jPanelAdmin.setBorder(BorderFactory.createLineBorder(Color.green, 3));
			_jPanelAdmin.setBounds(new Rectangle(261, 410, 303, 124));
			_jPanelAdmin.setVisible(true);
			_jPanelAdmin.add(_jLabelGwSender, null);
			_jPanelAdmin.add(_jLabel1BtId, null);
			_jPanelAdmin.add(getJScrollPanePorts(), null);
			_jPanelAdmin.add(_jLabelPorts, null);
			_jPanelAdmin.add(_jLabelDataPort, null);
			_jPanelAdmin.add(getJComboBoxBTDevice(), null);
			_jPanelAdmin.add(getJCheckBoxEnable(), null);
			_jPanelAdmin.add(getJScrollPaneGwSender(), null);
		}
		return _jPanelAdmin;
	}
	
	@Override
	public synchronized void newStream(NewStreamEvent event)
	{
		/*
		 * Ho ricevuto un nuovo stream da uno dei SessionManager.
		 */
		String streamName = event.get_streamName();
		
		PlayerManager playerManager = getPlayerManager(streamName);
		
		/*
		 * Se esiste un playerManager associato allo stream allora 
		 * ne aggiorno il file e il nome dello stream.
		 */
		if(playerManager != null)
		{
			playerManager.updatePlayer(event.getStartTime());
		}
		else
		{
			/*
			 * Se non esiste un playerManager associato allo stream 
			 * ne creo uno nuovo.
			 */
			
			// Sorgente dell'evento.
			SessionManager sessionManager =  (SessionManager)event.getSource();
			
			playerManager = new PlayerManager(event.getAbsolutePathName(), 
					event.get_streamName(),event.getLength(),
					(ISessionManager)sessionManager);
			
			synchronized(_synchPlayerManager)
			{
				_playerManagerVector.add(playerManager);
			}

			/*
			 * Abilito il bottone relativo allo stream.
			 */
			jButtonEnable(playerManager.getStreamName(), true);
		}
		
	}
	
	private String readTextFile(String fullPathFilename)
	{
		/*
		 * Il metodo legge il contenuto testuale di un file 
		 * e lo inserisce in una stringa restituita al chiamante.
		 */
		StringBuffer sb = new StringBuffer(1024);

		BufferedReader reader;

		try
		{
			reader = new BufferedReader(new FileReader(fullPathFilename));

			String stringReaded = null;

			do 
			{
				stringReaded = reader.readLine();

				if(stringReaded != null)
				{
					sb.append(stringReaded);	
					sb.append('\n');
				}
				
			}while(stringReaded != null);

			reader.close();
		} 
		catch (Exception e)
		{
			return null;
		}

		return sb.toString();
	}
	
	private PlayerManager getPlayerManager(String streamName)
	{
		/*
		 * Restituisce il playerManager della collezione 
		 * corrispondente allo streamName passato come argomento.
		 * Se non esiste restituisce null.
		 */
		synchronized(_synchPlayerManager)
		{
			for (PlayerManager playerManager: _playerManagerVector)
			{
				if(playerManager.getStreamName().contains(streamName) == true)
				{
					return playerManager;
				}

			}
		}

		return null;
	}
	
	private void jButtonEnable(String name, boolean value)
	{
		/*
		 * Abilita o disabilita il bottone con nome 
		 * coincidente con quello passato come argomento.
		 */
		for (Component component: getJPanelBackground().getComponents())
		{
			if(component instanceof JButton)
			{
				JButton jButton = (JButton)component;
				
				if(jButton.getName().equalsIgnoreCase(name) == true || name.equalsIgnoreCase("ALLBUTTON"))
				{
					jButton.setEnabled(value);
				}
			}
		}
	}
	
	private String[] configComboBoxDeviceID()
	{
		/*
		 * Restituisce un array di stringhe contenenti
		 * il device id di ogni dispositivo bluetooth.
		 */
		Vector<?> vector = null;
		
		try
		{
			vector = BlueCoveImpl.getLocalDevicesID();
		} 
		catch (BluetoothStateException e)
		{
			e.printStackTrace();
		}
		
		BlueCoveImpl.shutdown();
		
		if(vector != null)
		{
			int size = vector.size();
			String[] deviceIDstringArray = new String[size];
			
			int i;
			for(i=0; i<size; i++)
			{
				deviceIDstringArray[i] = vector.get(i).toString();
			}

			return deviceIDstringArray;
		}
		else
		{
			return null;
		}
	}
	
	private SessionManagerListener getSessionManagerListener()
	{
		return this;
	}
	
	private PortProviderListener getPortProviderListener()
	{
		return this;
	}
	
	private PlayerManagerListener getPlayerManagerListener()
	{
		return this;
	}

	@Override
	public void btPortUpdate(BtPortEvent btPortEvent)
	{
		getJListPorts().setListData(btPortEvent.get_btPsmArray());
	}
	
	/**
	 * This method initializes _jButtonGuide3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonGuide3()
	{
		if (_jButtonGuide3 == null)
		{
			_jButtonGuide3 = new JButton();
			_jButtonGuide3.setBounds(new Rectangle(226, 348, 94, 40));
			_jButtonGuide3.setText("Audio");
			_jButtonGuide3.setName("file3");
			_jButtonGuide3.setEnabled(false);
			_jButtonGuide3.setIcon(new ImageIcon("./media/audio.png"));
			_jButtonGuide3.setFont(new Font("DejaVu Sans", Font.PLAIN, 12));
			_jButtonGuide3.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mouseClicked(java.awt.event.MouseEvent e)
				{
					if(e.getComponent().isEnabled() == true)
					{
						mouseClickedButtonGuide(e.getComponent().getName());
					}
				}
			});
		}
		return _jButtonGuide3;
	}

	/**
	 * This method initializes _jPanelImage3	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImage3()
	{
		if (_jPanelImage3 == null)
		{
			_jPanelImage3 = new JPanelImage();
			_jPanelImage3.setBounds(new Rectangle(105, 304, 109, 83));
			_jPanelImage3.setName("file3-thumbnail");
			_jPanelImage3.setAbsolutePathImage("./media/file3.jpg");
		}
		return _jPanelImage3;
	}

	/**
	 * This method initializes _jScrollPaneTextGuide	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneTextGuide()
	{
		if (_jScrollPaneTextGuide == null)
		{
			_jScrollPaneTextGuide = new JScrollPane();
			_jScrollPaneTextGuide.setBounds(new Rectangle(6, 4, 248, 399));
			_jScrollPaneTextGuide.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.pink, 3), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.BELOW_TOP, new Font("DejaVu Sans", Font.BOLD, 10), SystemColor.activeCaption));
			_jScrollPaneTextGuide.setViewportView(getJTextGuide());
		}
		return _jScrollPaneTextGuide;
	}

	/**
	 * This method initializes _jPanelImageUser0	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageUser0()
	{
		if (_jPanelImageUser0 == null)
		{
			_jPanelImageUser0 = new JPanelImage();
			_jPanelImageUser0.setBounds(new Rectangle(414, 36, 61, 52));
			_jPanelImageUser0.setName("file0-user");
			_jPanelImageUser0.setAbsolutePathImage("./media/client_detected.png");
		}
		return _jPanelImageUser0;
	}

	/**
	 * This method initializes _jPanelImageUser4	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageUser4()
	{
		if (_jPanelImageUser4 == null)
		{
			_jPanelImageUser4 = new JPanelImage();
			_jPanelImageUser4.setBounds(new Rectangle(651, 53, 61, 52));
			_jPanelImageUser4.setAbsolutePathImage("./media/client_detected.png");
			_jPanelImageUser4.setName("file4-user");
			_jPanelImageUser4.setVisible(false);
		}
		return _jPanelImageUser4;
	}

	/**
	 * This method initializes _jPanelImageUser2	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageUser2()
	{
		if (_jPanelImageUser2 == null)
		{
			_jPanelImageUser2 = new JPanelImage();
			_jPanelImageUser2.setBounds(new Rectangle(208, 180, 61, 52));
			_jPanelImageUser2.setAbsolutePathImage("./media/client_detected.png");
			_jPanelImageUser2.setName("file2-user");
			_jPanelImageUser2.setVisible(false);
		}
		return _jPanelImageUser2;
	}

	/**
	 * This method initializes _jPanelImageUser3	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageUser3()
	{
		if (_jPanelImageUser3 == null)
		{
			_jPanelImageUser3 = new JPanelImage();
			_jPanelImageUser3.setBounds(new Rectangle(233, 292, 61, 52));
			_jPanelImageUser3.setAbsolutePathImage("./media/client_detected.png");
			_jPanelImageUser3.setName("file3-user");
			_jPanelImageUser3.setVisible(false);
		}
		return _jPanelImageUser3;
	}

	@Override
	public void portProviderNewPosition(NewPositionEvent event)
	{
		setPosition(event.get_newPosition());
	}
	

	private void setPosition(String position)
	{
		String suffixPosition = null;
		
		/*
		 * Segnaposto sulla mappa.
		 */
		suffixPosition = "-user";
		
		for (Component component: getJPanelBackground().getComponents())
		{
			if(component instanceof JPanelImage)
			{
				JPanelImage jPanelImage = (JPanelImage)component;
				
				if(jPanelImage.getName() != null && jPanelImage.getName().contains(suffixPosition))
				{
					if(jPanelImage.getName().contains(position + suffixPosition) == true)
					{
						jPanelImage.setVisible(true);
					}
					else
					{
						jPanelImage.setVisible(false);
					}
				}
			}
		}

		/*
		 * Bordo della miniatura in evidenza.
		 */
		suffixPosition = "-thumbnail";
		
		for (Component component: getJPanelBackground().getComponents())
		{
			if(component instanceof JPanelImage)
			{
				JPanelImage jPanelImage = (JPanelImage)component;
				
				if(jPanelImage.getName() != null && jPanelImage.getName().contains(suffixPosition))
				{
					if(jPanelImage.getName().contains(position + suffixPosition) == true)
					{
						jPanelImage
						.setBorder(BorderFactory.createLineBorder(Color.red, 3));
					}
					else
					{
						jPanelImage.setBorder(null);
					}
				}
			}
		}
		
		/*
		 * Immagine grande.
		 */
		getJPanelImageBig()
		.setAbsolutePathImage("./media/" + position + ".jpg");
		getJPanelImageBig()
		.setBorder(BorderFactory.createLineBorder(Color.red, 3));

		
		
		/*
		 * Guida testuale.
		 */
		String stringGuide = readTextFile("./media/" + position + ".txt");
		
		if(stringGuide != null)
		{
			getJTextGuide().setText(stringGuide);
			getJTextGuide().setCaretPosition(0);
			getJTextGuide().repaint();
		}
	}
	
	/**
	 * This method initializes _jPanelImage1	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImage1()
	{
		if (_jPanelImage1 == null)
		{
			_jPanelImage1 = new JPanelImage();
			_jPanelImage1.setBounds(new Rectangle(8, 88, 109, 88));
			_jPanelImage1.setName("file1-thumbnail");
			_jPanelImage1.setAbsolutePathImage("./media/file1.jpg");
		}
		return _jPanelImage1;
	}

	/**
	 * This method initializes _jToggleButtonAdmin	
	 * 	
	 * @return javax.swing.JToggleButton	
	 */
	private JToggleButton getJToggleButtonAdmin()
	{
		if (_jToggleButtonAdmin == null)
		{
			_jToggleButtonAdmin = new JToggleButton();
			_jToggleButtonAdmin.setBounds(new Rectangle(573, 515, 17, 19));
			_jToggleButtonAdmin.setFont(new Font("DejaVu Sans", Font.PLAIN, 8));
			_jToggleButtonAdmin.setHorizontalTextPosition(SwingConstants.CENTER);
			_jToggleButtonAdmin.setBackground(new Color(214, 217, 223));
			_jToggleButtonAdmin.setSelected(true);
			_jToggleButtonAdmin.setText("");
			_jToggleButtonAdmin.addItemListener(new java.awt.event.ItemListener()
			{
				public void itemStateChanged(java.awt.event.ItemEvent e)
				{
					
					if(e.getStateChange() == ItemEvent.SELECTED)
					{
						getJPanelAdmin().setVisible(true);
					}
					else
					{
						if(e.getStateChange() == ItemEvent.DESELECTED)
						{
							getJPanelAdmin().setVisible(false);
						}
					}
				}
			});
		}
		return _jToggleButtonAdmin;
	}

	/**
	 * This method initializes _jPanelImage0	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImage0()
	{
		if (_jPanelImage0 == null)
		{
			_jPanelImage0 = new JPanelImage();
			_jPanelImage0.setBounds(new Rectangle(313, 20, 90, 68));
			_jPanelImage0.setName("file0-thumbnail");
			_jPanelImage0.setAbsolutePathImage("./media/file0.jpg");
		}
		return _jPanelImage0;
	}

	/**
	 * This method initializes _jPanelImageUser1	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageUser1()
	{
		if (_jPanelImageUser1 == null)
		{
			_jPanelImageUser1 = new JPanelImage();
			_jPanelImageUser1.setBounds(new Rectangle(125, 73, 61, 52));
			_jPanelImageUser1.setAbsolutePathImage("./media/client_detected.png");
			_jPanelImageUser1.setName("file1-user");
			_jPanelImageUser1.setVisible(false);
		}
		return _jPanelImageUser1;
	}

	/**
	 * This method initializes _jProgressBarBuffer	
	 * 	
	 * @return javax.swing.JProgressBar	
	 */
	private JProgressBar getJProgressBarBuffer()
	{
		if (_jProgressBarBuffer == null)
		{
			_jProgressBarBuffer = new JProgressBar();
			_jProgressBarBuffer.setBounds(new Rectangle(12, 96, 226, 8));
			_jProgressBarBuffer.setString("");
			_jProgressBarBuffer.setMinimum(0);
			_jProgressBarBuffer.setMaximum(300);
			_jProgressBarBuffer.setValue(0);
			
			/*
			 * Field usati per il ridimensionamento della progressbar
			 */
			_jProgressBarX = _jProgressBarBuffer.getBounds().x;
			_jProgressBarWidth = _jProgressBarBuffer.getBounds().width;
		}
		return _jProgressBarBuffer;
	}


	/**
	 * This method initializes _jScrollPaneGwSender	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneGwSender()
	{
		if (_jScrollPaneGwSender == null)
		{
			_jScrollPaneGwSender = new JScrollPane();
			_jScrollPaneGwSender.setBounds(new Rectangle(124, 28, 174, 90));
			_jScrollPaneGwSender.setViewportView(getJTextAreaGwSender());
		}
		return _jScrollPaneGwSender;
	}

	/**
	 * This method initializes _jTextAreaGwSender	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreaGwSender()
	{
		if (_jTextAreaGwSender == null)
		{
			_jTextAreaGwSender = new JTextArea();
			_jTextAreaGwSender.setEditable(false);
		}
		return _jTextAreaGwSender;
	}
	
	private void mouseClickedButtonGuide(String name)
	{
		PlayerManager playerManager = getPlayerManager(name);
		
		if(_playerManager != null)
		{
			if(_playerManager.equals(playerManager) == true)
			{
				if(getJPanelPlayer().isVisible() == false)
				{
					getJPanelPlayer().setVisible(true);
				}
				return;
			}
			else
			{
				if(_playerManager.isPlayerPlaying())
				{
					_playerManager.pause();
				}

				_playerManager.removePlayerManagerListener(getPlayerManagerListener());	
			}
		}
		
		/* *
		 * Se il PlayerManger attuale è diverso da quello selezionato 
		 * allora lo aggiorno. 
		 */
		_playerManager = playerManager;
		
		_playerManager.addPlayerManagerListener(getPlayerManagerListener());
		
		_playerManager.setGainLevel(getJSliderGain().getValue());
		getJSliderTime().setMaximum(_playerManager.getStreamLength());
		
		if(getJPanelPlayer().isVisible() == false)
		{
			getJPanelPlayer().setVisible(true);
		}
		
		_jLabelStreamName.setText(_playerManager.getStreamName());
		_jButtonPlay.setText("Play");
		
		if(FileCompletedContainer.getInstance()
				.isStreamCompleted(_playerManager.getStreamName()) == true)
		{
			getJProgressBarBuffer()
			.setMaximum(_playerManager.getStreamLength() - _playerManager.getStartTime());
			getJProgressBarBuffer().setValue(getJProgressBarBuffer().getMaximum());
		}
	}

	@Override
	public void endReached(PlayerManagerEvent event)
	{
		if(event.getSource().equals(_playerManager))
		{
			resetPanelPlayer();
		}
	}

	@Override
	public void timeChanged(PlayerManagerEvent event)
	{
		if(event.getSource().equals(_playerManager) && _updateJSlider == true)
		{
			int currentTime = _playerManager.getCurrentTime();
			updateJLabelJSliderTime(currentTime);
		}
	}

	@Override
	public void waitData(PlayerManagerEvent event)
	{		
		PlayerManager playerManagerSource = (PlayerManager)event.getSource();
		_timer.schedule(new WaitingTask(playerManagerSource.getStreamName()), 0, 1000);
	}

	private void resetPanelPlayer()
	{
		getJButtonPlay().setText("Play");
		int currentTime = 0;

		if(_playerManager != null)
		{
			currentTime = _playerManager.getStartTime();	
		}
		
		updateJLabelJSliderTime(currentTime);
		
	}
	
	private void updateJLabelJSliderTime(int currentTime)
	{
		getJSliderTime().setValue(currentTime);
		
		int secondi = currentTime % 60;
		int minuti = (currentTime / 60) % 60;
		int ore = currentTime / 3600;

		StringBuffer stringBuffer = new StringBuffer();
		
		if(ore<10)
		{
			stringBuffer.append(0); 
		}
		
		stringBuffer.append(ore);
		stringBuffer.append(':');
		
		if(minuti<10)
		{
			stringBuffer.append(0);
		}
		
		stringBuffer.append(minuti);
		stringBuffer.append(':');
		
		if(secondi<10)
		{
			stringBuffer.append(0);
			
		}
		stringBuffer.append(secondi);
		
		_jLabelTime.setText(stringBuffer.toString());
	}	

	@Override
	public void newDataReceived(NewDataReceivedEvent event)
	{
		
		if(_playerManager != null && event.getStreamName()
				.equalsIgnoreCase(_playerManager.getStreamName()))
		{
			
			getJProgressBarBuffer()
			.setMaximum(event.getStreamLength() - event.getStartTime());
			
			
			int shiftFactor = (event.getStartTime() * 
					_jProgressBarWidth) / event.getStreamLength();
			
			getJProgressBarBuffer().setBounds(_jProgressBarX + shiftFactor,
					96, _jProgressBarWidth - shiftFactor, 8);
			getJProgressBarBuffer().setValue(event.getFileLength());
		}
		
	}

	@Override
	public void portProvideNewSender(NewSenderEvent event)
	{
		_jTextAreaGwSender.append(event.get_gatewaySender() + 
				" "+ event.get_dataPort() + "\n");
	}

	/**
	 * This method initializes _jProgressBarBufferBack	
	 * 	
	 * @return javax.swing.JProgressBar	
	 */
	private JProgressBar getJProgressBarBufferBack()
	{
		if (_jProgressBarBufferBack == null)
		{
			_jProgressBarBufferBack = new JProgressBar();
			_jProgressBarBufferBack.setBounds(new Rectangle(12, 96, 226, 8));
			_jProgressBarBufferBack.setMaximum(300);
			_jProgressBarBufferBack.setString("");
		}
		return _jProgressBarBufferBack;
	}
	
	private void resetJProgressBarBuffer()
	{
		_jProgressBarBuffer.setBounds(new Rectangle(12, 96, 226, 8));
		_jProgressBarBuffer.setValue(0);
	}

	@Override
	public void streamError(StreamErrorEvent event)
	{
		closeJPanelPlayer(event.getStreamName());
		
		JPanelImage jPanelImage = getJPanelImage(event.getStreamName() + "-error");
		
		if(jPanelImage != null)
		{
			_timer.schedule(new ErrorTask(jPanelImage), 0, 1000);
		}

	}
	
	private JPanelImage getJPanelImage(String name)
	{
		JPanelImage jPanelImage = null;
		
		for (Component component: getJPanelBackground().getComponents())
		{
			if(component instanceof JPanelImage)
			{
				JPanelImage jPanelImg = (JPanelImage)component;

				if(jPanelImg.getName() != null && jPanelImg.getName().contains(name) == true)
				{
					jPanelImage = jPanelImg;
				}
			}
		}
		
		return jPanelImage;
	}

	
	class ErrorTask extends TimerTask
	{
		private int counter = 0;
		private JPanelImage _jPanelImage = null;
		
		public ErrorTask(JPanelImage jPanelImage)
		{
			_jPanelImage = jPanelImage;
		}

		public void run()
		{
			counter++;
			
			if((counter % 2) == 1)
			{
				_jPanelImage.setVisible(true);
			}
			else
			{
				_jPanelImage.setVisible(false);
				
			}
			
			if(counter > 5)
			{
				this.cancel();
			}
			
		}
	}

	class WaitingTask extends TimerTask
	{
		private int counter = -1;
		private String _streamName = null;
		
		public WaitingTask(String streamName)
		{
			_streamName = streamName;
		}
		
		public void run()
		{
			counter++;
		
			JPanelImage jPanelImage = getJPanelImage(_streamName + "-waiting");
			PlayerManager playerManager = getPlayerManager(_streamName);

			if(jPanelImage != null && playerManager != null)
			{
				if(playerManager.isWaitingData() == true)
				{

					jPanelImage.setVisible(true);
					jPanelImage.setAbsolutePathImage("./media/progress" + counter + ".jpg");

				}
				else
				{
					jPanelImage.setVisible(false);
					
					this.cancel();
				}
			}
			else
			{
				jPanelImage.setVisible(false);
				this.cancel();
			}


			if(counter == 3)
			{
				counter = -1;
			}
			
		}
	}

	/**
	 * This method initializes _jPanelImageError1	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageError1()
	{
		if (_jPanelImageError1 == null)
		{
			_jPanelImageError1 = new JPanelImage();
			_jPanelImageError1.setBounds(new Rectangle(191, 70, 26, 25));
			_jPanelImageError1.setAbsolutePathImage("./media/error.png");
			_jPanelImageError1.setName("file1-error");
			_jPanelImageError1.setVisible(false);
		}
		return _jPanelImageError1;
	}

	/**
	 * This method initializes jPanelImage2	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImage22()
	{
		if (_jPanelImageError2 == null)
		{
			_jPanelImageError2 = new JPanelImage();
			_jPanelImageError2.setBounds(new Rectangle(278, 181, 26, 25));
			_jPanelImageError2.setAbsolutePathImage("./media/error.png");
			_jPanelImageError2.setName("file2-error");
			_jPanelImageError2.setVisible(false);
		}
		return _jPanelImageError2;
	}

	/**
	 * This method initializes _jPanelImageError3	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageError3()
	{
		if (_jPanelImageError3 == null)
		{
			_jPanelImageError3 = new JPanelImage();
			_jPanelImageError3.setBounds(new Rectangle(306, 298, 26, 25));
			_jPanelImageError3.setAbsolutePathImage("./media/error.png");
			_jPanelImageError3.setName("file3-error");
			_jPanelImageError3.setVisible(false);
		}
		return _jPanelImageError3;
	}

	/**
	 * This method initializes jPanelImage4	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImage42()
	{
		if (_jPanelImageError4 == null)
		{
			_jPanelImageError4 = new JPanelImage();
			_jPanelImageError4.setBounds(new Rectangle(602, 51, 26, 25));
			_jPanelImageError4.setAbsolutePathImage("./media/error.png");
			_jPanelImageError4.setName("file4-error");
			_jPanelImageError4.setVisible(false);
		}
		return _jPanelImageError4;
	}

	/**
	 * This method initializes _jPanelImageWaiting1	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageWaiting1()
	{
		if (_jPanelImageWaiting1 == null)
		{
			_jPanelImageWaiting1 = new JPanelImage();
			_jPanelImageWaiting1.setBounds(new Rectangle(191, 102, 26, 25));
			_jPanelImageWaiting1.setAbsolutePathImage("./media/progress0.jpg");
			_jPanelImageWaiting1.setName("file1-waiting");
			_jPanelImageWaiting1.setVisible(false);
		}
		return _jPanelImageWaiting1;
	}

	/**
	 * This method initializes _jPanelImageWaiting2	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageWaiting2()
	{
		if (_jPanelImageWaiting2 == null)
		{
			_jPanelImageWaiting2 = new JPanelImage();
			_jPanelImageWaiting2.setBounds(new Rectangle(279, 210, 26, 25));
			_jPanelImageWaiting2.setAbsolutePathImage("./media/progress0.jpg");
			_jPanelImageWaiting2.setName("file2-waiting");
			_jPanelImageWaiting2.setVisible(false);
		}
		return _jPanelImageWaiting2;
	}

	/**
	 * This method initializes _jPanelImageWaiting3	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageWaiting3()
	{
		if (_jPanelImageWaiting3 == null)
		{
			_jPanelImageWaiting3 = new JPanelImage();
			_jPanelImageWaiting3.setBounds(new Rectangle(304, 324, 26, 25));
			_jPanelImageWaiting3.setAbsolutePathImage("./media/progress0.jpg");
			_jPanelImageWaiting3.setName("file3-waiting");
			_jPanelImageWaiting3.setVisible(false);
		}
		return _jPanelImageWaiting3;
	}

	/**
	 * This method initializes _jPanelImageWaiting4	
	 * 	
	 * @return commonUtility.JPanelImage	
	 */
	private JPanelImage getJPanelImageWaiting4()
	{
		if (_jPanelImageWaiting4 == null)
		{
			_jPanelImageWaiting4 = new JPanelImage();
			_jPanelImageWaiting4.setBounds(new Rectangle(601, 82, 26, 25));
			_jPanelImageWaiting4.setAbsolutePathImage("./media/progress0.jpg");
			_jPanelImageWaiting4.setName("file4-waiting");
			_jPanelImageWaiting4.setVisible(false);
		}
		return _jPanelImageWaiting4;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"